/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author David
 */
public class ObserverInterestMap {

    private HashMap<ObserverInterest, List<Observer>> map;

    public ObserverInterestMap() {
        this.map = new HashMap<ObserverInterest, List<Observer>>();
    }

    public void put(ObserverInterest interest, Observer observer) {
        if (!this.map.containsKey(interest)) {
            this.map.put(interest, new ArrayList<Observer>());
        }

        this.map.get(interest).add(observer);
    }

    public boolean remove(Observer observer) {
        for (List<Observer> list : this.map.values()) {
            if (list.contains(observer)) {
                list.remove(observer);
                return true;
            }
        }
        return false;
    }

    public void notifyObservers(ObserverInterest interest) {
        if (map.size() > 0) {
            if(map.get(interest) != null){
                for(Observer observer : map.get(interest)){
                    observer.notifyChange(interest);
                }
            }
        }
    }
}
